package com.zzz.system.controller;

import com.zzz.framework.model.AmisSource;
import com.zzz.framework.model.PageResult;
import com.zzz.framework.model.R;
import com.zzz.system.service.TemplateService;
import com.zzz.system.vo.PreviewTemplateVO;
import com.zzz.system.vo.QueryTemplateVO;
import com.zzz.system.vo.SaveTemplateVO;
import com.zzz.system.vo.TemplateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("api")
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    @GetMapping("template/listSelect")
    public R<AmisSource> listSelect(@RequestParam("groupCode") String groupCode) {
        return R.data(AmisSource.of(templateService.findByGroupCode(groupCode), TemplateVO::getCode, TemplateVO::getCode));
    }

    @PostMapping("template/findByCondition")
    public R<PageResult<TemplateVO>> findByCondition(@RequestBody @NotNull @Validated QueryTemplateVO queryTemplateVO) {
        return R.data(templateService.findByCondition(queryTemplateVO));
    }

    @GetMapping("template/findById")
    public R<TemplateVO> findById(@RequestParam("id") Long id,
                                  @RequestParam(value = "dataSourceCode", required = false) String dataSourceCode,
                                  @RequestParam(value = "tableName", required = false) String tableName) {
        return R.data(templateService.findById(id, dataSourceCode, tableName));
    }

    @GetMapping("template/findByGroupCode")
    public R findByGroupCode(@RequestParam("groupCode") String groupCode,
                             @RequestParam(value = "dataSourceCode", required = false) String dataSourceCode,
                             @RequestParam(value = "tableName", required = false) String tableName) {
        List<TemplateVO> templateVOList = templateService.findByGroupCode(groupCode, dataSourceCode, tableName);
        return R.data(new HashMap() {{
                          put("templates", templateVOList);
                      }}
        );
    }

    @GetMapping("template/findByGroupCodeAndCode")
    public R<TemplateVO> findByGroupCodeAndCode(@RequestParam("groupCode") String groupCode,
                                                @RequestParam("code") String code) {
        return R.data(templateService.findByGroupCodeAndCode(groupCode, code));
    }

    @PostMapping("template/previewByContent")
    public R<TemplateVO> previewByContent(@RequestBody @NotNull @Validated PreviewTemplateVO previewTemplateVO) {
        return R.data(templateService.previewByContent(previewTemplateVO));
    }

    @PostMapping("template/edit/{id}")
    public R edit(@PathVariable Long id, @RequestBody @Validated @NotNull SaveTemplateVO saveTemplateVO) {
        saveTemplateVO.setId(id);
        return templateService.edit(saveTemplateVO);
    }

    @PostMapping("template/add")
    public R add(@RequestBody @Validated @NotNull SaveTemplateVO saveTemplateVO) {
        return templateService.add(saveTemplateVO);
    }

    @PostMapping("template/remove/{id}")
    public R remove(@PathVariable Long id) {
        return templateService.remove(id);
    }

}
